<template>
  <div class="bottom-bar">
    <div  class="check-content">
      <check-button class="check-button" :is-checked="isSelectAll" @click.native="checkClick"/>
      <span>全选</span>
    </div>
    <div class="price">合计：{{totalPrice}}</div>
    <div class="calculate" @click="calClick">
      去计算({{ checkLength}})
    </div>
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton";
import {mapGetters} from "vuex"
export default {
  name: "CartBottomBar",
  components: {
    CheckButton
  },

  computed: {
    ...mapGetters({
      cartList: 'cartList'
    }),
    totalPrice() {
      return '￥' + this.cartList.filter(item => {
        return item.checked
      }).reduce((preValue,item) => {
        return preValue + item.price * item.count
      },0).toFixed(2)
    },
    checkLength() {
      return this.cartList.filter(item => item.checked).length
    },
    isSelectAll() {
      // return !(this.cartList.filter(item => !item.checked).length)
      if(this.cartList.length === 0)
        return false
      // return !this.cartList.find(item => !item.checked)
      for(let item of this.cartList) {
        if(!item.checked) {
          return false
        }
      }
      return true
    }
  },
  methods: {
    checkClick() {
      if(this.isSelectAll) {
        this.cartList.forEach(item => item.checked = false)
      }
      else {
        this.cartList.forEach(item => item.checked = true)
      }
    },
    calClick() {
      if (this.$store.state.cartList.length === 0){
        this.$toast.show('购物车为空')
      }
    }
  }
}
</script>

<style scoped>
  .bottom-bar {
    position: relative;
    height: 40px;
    line-height: 40px;
    background-color: #eee;
    display: flex;
    font-size: 16px;
  }
  .check-content {
    display: flex;
    width: 60px;
    align-items: center;
    margin-left: 5px;
  }
  .check-button {
    width: 20px;
    height: 20px;
    line-height: 20px;
    margin-right: 5px;
  }
  .price {
    line-height: 40px;
    margin-left: 20px;
    flex: 1;
  }
  .calculate {
    color: #fff;
    line-height: 40px;
    text-align: center;
    width: 90px;
    height: 100%;
    background-color: orangered;
  }
</style>
